package com.css.fxfzypg.modules.suggestinfo.analysis.annualrisk.service.impl;


import com.css.fxfzypg.modules.suggestinfo.analysis.annualrisk.repository.AnnualRiskRepository;
import com.css.fxfzypg.modules.suggestinfo.analysis.annualrisk.repository.entity.AnnualRiskEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.annualrisk.service.AnnualRiskService;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.SysMenuVo;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.UUIDGenerator;
import com.css.fxfzypg.web.PlatformSessionContext;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Service
class AnnualRiskServiceImpl implements AnnualRiskService {
    @Autowired
    AnnualRiskRepository annualRiskRepository;

    @Autowired
    SysAreaService sysAreaService;

    @Autowired
    SUserService sUserService;

    @Transactional
    @Override
    public Boolean save(AnnualRiskEntity annualRiskEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        annualRiskEntity.setId(uuid);
        String divisionFlag = annualRiskEntity.getDivisionFlag();
        if(divisionFlag.equals("0")){//按区县
            if(PlatformObjectUtils.isEmpty(annualRiskEntity.getProvinceId())||PlatformObjectUtils.isEmpty(annualRiskEntity.getCityId())||PlatformObjectUtils.isEmpty(annualRiskEntity.getAreaId())) {
                String ids = sysAreaService.getIdByCondition(URLEncoder.encode(annualRiskEntity.getProvince(), "UTF-8"), URLEncoder.encode(annualRiskEntity.getCity(), "UTF-8"), URLEncoder.encode(annualRiskEntity.getArea(), "UTF-8"));
                annualRiskEntity.setProvinceId(ids.split("_")[0]);
                annualRiskEntity.setCityId(ids.split("_")[1]);
                annualRiskEntity.setAreaId(ids.split("_")[2]);
            }
        }else if(divisionFlag.equals("1")){//按市
            String provinceId = "";
            String cityId = "";
            List<SysMenuVo> sysMenuList = getSAreaTree("1");//获取所有省
            for(SysMenuVo sysMenuVo : sysMenuList) {
                String provinceName = sysMenuVo.getName();
                if (annualRiskEntity.getProvince().equals(provinceName)) {
                    provinceId = sysMenuVo.getValue();
                    List<SysMenuVo> citySysMenu = getSAreaTree(provinceId);//获取该省下所有市
                    for (SysMenuVo citySysMenuVo : citySysMenu) {
                        if(citySysMenuVo.getName().equals(annualRiskEntity.getCity())){
                            cityId = citySysMenuVo.getValue();
                        }
                    }
                }
            }
            annualRiskEntity.setProvinceId(provinceId);
            annualRiskEntity.setCityId(cityId);
        }
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        annualRiskEntity.setCreateId(sUser.getUserId());
        annualRiskEntity.setCreateName(sUser.getUserName());
        annualRiskEntity.setDelFlag("0");
        annualRiskEntity.setCreateTime(new Date());
        return annualRiskRepository.save(annualRiskEntity);
    }

    private List<SysMenuVo> getSAreaTree(String divisionId) throws UnsupportedEncodingException {
        String jsonStr = sysAreaService.jsonSAreaTree(URLEncoder.encode(divisionId, "UTF-8"));
        Gson gson = CreateGson.createGson();
        Type srItemsStandardType = new TypeToken<List<SysMenuVo>>() {
        }.getType();
        List<SysMenuVo> sysMenuList = gson.fromJson(jsonStr, srItemsStandardType);
        return sysMenuList;
    }

    @Transactional
    @Override
    public Boolean update(AnnualRiskEntity annualRiskEntity) {
        return annualRiskRepository.update(annualRiskEntity);
    }

    @Override
    public AnnualRiskEntity getAnnualRiskData(ParamsVo params) {
        return annualRiskRepository.getAnnualRiskData(params);
    }

    @Override
    public List<AnnualRiskEntity> getAnnualRiskDataByTaskNum(String taskNum) {
        return annualRiskRepository.getAnnualRiskDataByTaskNum(taskNum);
    }

    @Override
    public AnnualRiskEntity getAnnualRiskDataByTaskNum(String taskNum, String cityId, String areaId) {
        return annualRiskRepository.getAnnualRiskDataByTaskNum(taskNum,cityId,areaId);
    }

    @Override
    public List<AnnualRiskEntity> getAnnualRiskAreaData(ParamsVo paramsVo) {
        return annualRiskRepository.getAnnualRiskAreaData(paramsVo);
    }

    @Override
    public int getCountById(String id) {
        return annualRiskRepository.getCountById(id);
    }
}
